/**
 * 
 * @author  phoenics@126.com
 * @version v.0.1
 * @date  2018年3月14日 下午2:04:03 
 * 
 */
package com.github.phoenics.militia.common.auth;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import java.util.Collection;
import java.util.Map;

import static java.util.Collections.emptyList;

/**
 *
 */
public class TokenAuthentication<T extends PasswordText> implements Authentication{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8686255739940068702L;
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TokenAuthentication.class);
	private String token;  
    private T user;
    private Map<String, Object> details;  
    public TokenAuthentication(T user, String token, Map<String, Object> details){
        this.token = token;  
        this.user = user;
        this.details = details;  
    }  
  
//    public Optional<T> user(){
//        return Optional.ofNullable(user);
//    }
  
  
    @Override  
    public Collection<? extends GrantedAuthority> getAuthorities() {  
        return emptyList();  
    }  
  
    @Override  
    public Object getCredentials() {  
        return token;  
    }  
  
    @Override  
    public Object getDetails() {  
        return details;  
    }  
  
    @Override  
    public Object getPrincipal() {  
        return user;  
    }  
  
    @Override  
    public boolean isAuthenticated() {  
        return user != null  && user.getTickets() != null;
    }  
  
    @Override  
    public void setAuthenticated(boolean b) throws IllegalArgumentException {  
        if (!isAuthenticated()){  
            user = null;  
        }  
    }  
  
    @Override  
    public String getName() {  
        return user.getTickets();
    }  
  
    @Override  
    public String toString() {  
        return token;  
    }  
}
